const app = getApp();
let leftHeight = 0,
 rightHeight = 0; //分别定义左右两边的高度
let query;
const db = wx.cloud.database()
const {get_teacher_info} = require('../../Class/get_teacher_info.js');
const {get_class_info} = require('../../Class/get_class_video.js');
const {get_others} = require('../../Class/GetOthers.js');
const teacherInfo = new get_teacher_info();
const classInfo = new get_class_info();
const getOthers = new get_others();

Page({
  data: {
    avatar: '',
    ScreenHeight: "",
    navbarHeight: '',
    num: 0,
    rocket: 0,
    btn: false,
    van_tab: ['汉语言文学', '新闻学', '网络与新媒体'],
  },

  GetTime() {
    const{year,days_remaining} = getOthers.Get_RemainderTime()
    this.setData({year,days_remaining})
  },
  GoHead(e) {
    wx.pageScrollTo({scrollTop: 0})
  },
  GoItem(e){
    var id = parseInt(e.currentTarget.dataset.id)
    if (id == 0){wx.navigateTo({url: '/pages/wenchuan/wenchuan',})
    }else if(id == 1){wx.navigateTo({url: '/pages/wencui/index',})
    }else if(id == 2){wx.navigateTo({url: '/pages/official-account/official-account',})
    }else if(id == 3){
      wx.showModal({
        title: '暂时未开发此区域',
        content: '欢迎来客服区域建言献策！',
        showCancel:false
      })
      // wx.navigateTo({url: '/pages/study/study',})
    }
  },
  TeacherInfo(e) {
    var id = e.currentTarget.dataset.id
    var tabid = this.data.userid
    wx.navigateTo({
      url: '/pages/teachers-team/introduce/introduce?tabid='+tabid + '&teacherid=' + id,
    })
  },

  GetTeacher(e) {
    if (e == null) {
      var userid = 0
    } else {
      var userid = parseInt(e.detail.index)
    }
    const user_result = teacherInfo.GetUserChoice(userid,this.data.hanyuyan,this.data.xinwen,this.data.wangxin);
    this.setData({
      teacher : user_result,
      userid,
    })
  },

  MoreTeacher() {
    wx.navigateTo({
      url: '/pages/teachers-team/teachers-team',
    })
  },
  MoreClass(){
    wx.switchTab({
      url: '/pages/OnlineClass/OnlineClass',
    })
  },

  async onLoad(options) {
    wx.showLoading({
      title: '数据通信中',
      mask: true
    })
    // new_user, 
    const [teacher_result, class_result,navbarHeight] = await Promise.all([
      teacherInfo.GetTeacherInfo(),
      classInfo.GetClass_index(),
      getOthers.Set_navbar(),
    ]);
  
    this.setData({
      hanyuyan: teacher_result.hanyuyan,
      xinwen: teacher_result.xinwen,
      wangxin: teacher_result.wangxin,
      hyy_class: class_result.hyy_lesson.slice(12, 44),
      xinwen_class: class_result.xinwen_lesson.slice(0, 30),
      navbarHeight
    });
  
    app.globalData.teacher_result = teacher_result;
    app.globalData.class_result = class_result;
  
    this.GetTime();
    this.GetTeacher(null);
    this.GetClass(null);
  
    wx.hideLoading()
  },
  
  // 监听页面
  onPageScroll(e) {
    var user = e.scrollTop
    if (user <= 100) {
      this.setData({
        num: 0,
        btn: false,
        rocket: 0
      })
    } else if (100 < user < 300) {
      this.setData({
        num: user / 300,
        btn: true,
        rocket: 0.8
      })
    } else if (user > 300) {
      this.setData({
        num: 1,
        btn: true,
        rocket: 0.8
      })
    }
  },

  onShow: function () {
    const user = wx.getStorageSync('UserData')
    this.setData({avatar :user.avatar})
  },

  GetClass(e){
    if(e == null){
      var id = 0 
    }else{
      var id = parseInt(e.detail.index)
    }
    switch(id){
      case 0:
        this.setData({
          class_id : id,
          class : this.data.hyy_class
        })
        break;
      case 1:
        this.setData({
          class_id : id,
          class : this.data.xinwen_class
        })
        break;
    }
  },

  GoClass(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/OnlineClass/classvideo/classvideo?video_info='+ JSON.stringify(this.data.class[id]),
    })
  },

  // 瀑布流布局方法（后面看看怎么封装）
  // async isLeft() {
  //   const list = []
  //   console.log(this.data.class)
  //   for (let index = 0; index < this.data.class.length; index++) {
  //     list.push({title:this.data.class[index].text,url:this.data.class[index].img})
  //   }
  //   const leftList = []
  //   const rightList = []
  //   query = wx.createSelectorQuery();
  //   for (const item of list) {
  //     leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
  //     await this.getBoxHeight(leftList, rightList);
  //   }
  // },
  // getBoxHeight(leftList, rightList) { //获取左右两边高度
  //   return new Promise((resolve, reject) => {
  //     this.setData({
  //       leftList,
  //       rightList
  //     }, () => {
  //       query.select('#left').boundingClientRect();
  //       query.select('#right').boundingClientRect();
  //       query.exec((res) => {
  //         leftHeight = res[0].height; //获取左边列表的高度
  //         rightHeight = res[1].height; //获取右边列表的高度
  //         resolve();
  //       });
  //     });
  //   })
  // }
})